/*
 * IJA2011, Board game "Clovece nezlob se"
 * grp108
 * Tomáš Mičiak,xmicia00
 * Andrej Martinák, xmarti51
 *
 * Created on 5.5.2011, 10:38:06
 */

package src.clovece;

import java.awt.Point;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import src.clovece.game.GameInfo;
import src.clovece.game.Multiplayer;

/**
 *
 * @author Thomas
 */
public class NewMultiplayerDialog extends javax.swing.JDialog {
    private GameInfo gameInfo = GameInfo.getGameInfoInstance();
    private GameFrame parent = null;
    private Multiplayer mp = new Multiplayer();
    private JLabel label1 = null;
    private JLabel label2 = null;
    private JLabel label3 = null;
    private JLabel label4 = null;

    /** Creates new form NewMultiplayerDialog */
    public NewMultiplayerDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        this.parent = (GameFrame) parent;
        Point p = parent.getLocation();
        this.setLocation(p.x +300, p.y +150);
    }

    public void showYourself() {
        initComponents();
        
        ButtonGroup group = new ButtonGroup();
        group.add(this.RadioButton_player1);
        group.add(this.RadioButton_player2);
        group.add(this.RadioButton_player3);
        group.add(this.RadioButton_player4);

        if(this.gameInfo.getChosenFigurine() == 0) this.RadioButton_player1.setSelected(true);
        else if(this.gameInfo.getChosenFigurine() == 1) this.RadioButton_player2.setSelected(true);
        else if(this.gameInfo.getChosenFigurine() == 2) this.RadioButton_player3.setSelected(true);
        else if(this.gameInfo.getChosenFigurine() == 3) this.RadioButton_player4.setSelected(true);

        if(this.gameInfo.getGameBoardSize() == 36) this.GameBoardSizeComboBox.setSelectedIndex(0);
        else if(this.gameInfo.getGameBoardSize() == 48) this.GameBoardSizeComboBox.setSelectedIndex(1);
        if(this.gameInfo.getGameBoardSize() == 60) this.GameBoardSizeComboBox.setSelectedIndex(2);

        if(this.gameInfo.getNumberOfPlayers() == 2) this.NumberOfPlayersComboBox.setSelectedIndex(0);
        else if(this.gameInfo.getNumberOfPlayers() == 3) this.NumberOfPlayersComboBox.setSelectedIndex(1);
        else if(this.gameInfo.getNumberOfPlayers() == 4) this.NumberOfPlayersComboBox.setSelectedIndex(2);

        this.label1 = new JLabel();
        this.label2 = new JLabel();
        this.label3 = new JLabel();
        this.label4 = new JLabel();


        this.label1.setIcon(Figurine(this.gameInfo.getGraphicMode(), 0));
        this.label1.setBounds(140,13,40,40);

        this.label2.setIcon(Figurine(this.gameInfo.getGraphicMode(), 1));
        this.label2.setBounds(140,52,40,40);

        this.label3.setIcon(Figurine(this.gameInfo.getGraphicMode(), 2));
        this.label3.setBounds(210,13,40,40);

        this.label4.setIcon(Figurine(this.gameInfo.getGraphicMode(), 3));
        this.label4.setBounds(210,52,40,40);

        this.GraphicModeComboBox.setSelectedIndex(this.gameInfo.getGraphicMode());

        this.GraphicsPanel.add(label1);
        this.GraphicsPanel.add(label2);
        this.GraphicsPanel.add(label3);
        this.GraphicsPanel.add(label4);

        this.GraphicModeComboBox.setSelectedIndex(this.gameInfo.getGraphicMode());

        LoginDialog ld = new LoginDialog(this, true);
        ld.showYourself();
    }

    /*
     * Loads image of a figurine
     * @param int graphic set
     * @param int figurine
     */
    private ImageIcon Figurine(int gameSet, int figurine)
    {
        ImageIcon icon = new ImageIcon(System.getProperty("user.dir")+ System.getProperty("file.separator")
                  + "lib" + System.getProperty("file.separator") + "img"
                  + System.getProperty("file.separator") + "figurine" + gameSet + figurine + "a" + ".png");
        return icon;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        GameBoardSizeLabel = new javax.swing.JLabel();
        NumberOfPlayersLabel = new javax.swing.JLabel();
        GameBoardSizeComboBox = new javax.swing.JComboBox();
        NumberOfPlayersComboBox = new javax.swing.JComboBox();
        GraphicsPanel = new javax.swing.JPanel();
        RadioButton_player2 = new javax.swing.JRadioButton();
        RadioButton_player1 = new javax.swing.JRadioButton();
        GraphicModeComboBox = new javax.swing.JComboBox();
        RadioButton_player3 = new javax.swing.JRadioButton();
        RadioButton_player4 = new javax.swing.JRadioButton();
        StartGameButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Game", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));

        GameBoardSizeLabel.setText("Game board size");

        NumberOfPlayersLabel.setText("Number of players");

        GameBoardSizeComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "small", "medium", "large" }));

        NumberOfPlayersComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "2", "3", "4" }));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(NumberOfPlayersLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 116, Short.MAX_VALUE)
                        .addComponent(NumberOfPlayersComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(GameBoardSizeLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 85, Short.MAX_VALUE)
                        .addComponent(GameBoardSizeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(4, 4, 4)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(GameBoardSizeLabel)
                    .addComponent(GameBoardSizeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(NumberOfPlayersLabel)
                    .addComponent(NumberOfPlayersComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        GraphicsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Graphics", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));

        GraphicModeComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Mode 1", "Mode 2" }));
        GraphicModeComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                GraphicModeComboBoxActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout GraphicsPanelLayout = new javax.swing.GroupLayout(GraphicsPanel);
        GraphicsPanel.setLayout(GraphicsPanelLayout);
        GraphicsPanelLayout.setHorizontalGroup(
            GraphicsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(GraphicsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(GraphicModeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(32, 32, 32)
                .addGroup(GraphicsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(RadioButton_player1)
                    .addComponent(RadioButton_player2))
                .addGap(50, 50, 50)
                .addGroup(GraphicsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(RadioButton_player3)
                    .addComponent(RadioButton_player4))
                .addContainerGap(76, Short.MAX_VALUE))
        );
        GraphicsPanelLayout.setVerticalGroup(
            GraphicsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(GraphicsPanelLayout.createSequentialGroup()
                .addGroup(GraphicsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(RadioButton_player1)
                    .addComponent(GraphicModeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(RadioButton_player3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
                .addGroup(GraphicsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(RadioButton_player2)
                    .addComponent(RadioButton_player4)))
        );

        StartGameButton.setText("Start Game");
        StartGameButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                StartGameButtonMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(GraphicsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(121, 121, 121)
                        .addComponent(StartGameButton, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(GraphicsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, Short.MAX_VALUE)
                .addComponent(StartGameButton)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void GraphicModeComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_GraphicModeComboBoxActionPerformed
    if(this.GraphicModeComboBox.getSelectedItem().toString().equals("Mode 1")) {
            this.setGraphicMode1();
        }
        else {
            this.setGraphicMode2();
        }
    }//GEN-LAST:event_GraphicModeComboBoxActionPerformed

    private void StartGameButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_StartGameButtonMouseClicked
        this.setVisible(false);

        // set new multiplayer game
        this.gameInfo.setNewGame(true);

        // game board size
        if(this.GameBoardSizeComboBox.getSelectedItem().toString().equals("small")) {
            this.gameInfo.setGameBoardSize(36);
        }
        else if(this.GameBoardSizeComboBox.getSelectedItem().toString().equals("medium")) {
            this.gameInfo.setGameBoardSize(48);
        }
        else if(this.GameBoardSizeComboBox.getSelectedItem().toString().equals("large")) {
            this.gameInfo.setGameBoardSize(60);
        }

        // number of players
        this.gameInfo.setNumberOfPlayers(Integer.parseInt(this.NumberOfPlayersComboBox.getSelectedItem().toString()));

        // singleplayer / multiplater
        this.gameInfo.setLocalGame(false);

        

        // graphic mode
        if(this.GraphicModeComboBox.getSelectedItem().toString().equals("Mode 1")) {
            this.gameInfo.setGraphicMode(0);
        }
        else {
            this.gameInfo.setGraphicMode(1);
        }

        // chosen figurine
        if(this.RadioButton_player1.isSelected()) {
            this.gameInfo.setChosenFigurine(0);
        }
        else if(this.RadioButton_player2.isSelected()) {
            this.gameInfo.setChosenFigurine(1);
        }
        else if(this.RadioButton_player3.isSelected()) {
            this.gameInfo.setChosenFigurine(2);
        }
        else {
            this.gameInfo.setChosenFigurine(3);
        }


        this.mp.writeNewGame(String.valueOf(gameInfo.getNumberOfPlayers()), String.valueOf(gameInfo.getGameBoardSize()));
        WaitDialog wd = new WaitDialog(parent,true);
        wd.setVisible(true);

        //TODO doplnit ulozenie grafickych nastaveni
        this.parent.fillGameFrame();
        this.dispose();

    }//GEN-LAST:event_StartGameButtonMouseClicked

    /**
     * Loads figurines from first graphic set
     */
    private void setGraphicMode1() {
        this.label1.setIcon(this.Figurine(0, 0));
        this.label2.setIcon(this.Figurine(0, 1));
        this.label3.setIcon(this.Figurine(0, 2));
        this.label4.setIcon(this.Figurine(0, 3));
    }

    /**
     * Loads figurines from second graphic set
     */
    private void setGraphicMode2() {
        this.label1.setIcon(this.Figurine(1, 0));
        this.label2.setIcon(this.Figurine(1, 1));
        this.label3.setIcon(this.Figurine(1, 2));
        this.label4.setIcon(this.Figurine(1, 3));
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox GameBoardSizeComboBox;
    private javax.swing.JLabel GameBoardSizeLabel;
    private javax.swing.JComboBox GraphicModeComboBox;
    private javax.swing.JPanel GraphicsPanel;
    private javax.swing.JComboBox NumberOfPlayersComboBox;
    private javax.swing.JLabel NumberOfPlayersLabel;
    private javax.swing.JRadioButton RadioButton_player1;
    private javax.swing.JRadioButton RadioButton_player2;
    private javax.swing.JRadioButton RadioButton_player3;
    private javax.swing.JRadioButton RadioButton_player4;
    private javax.swing.JButton StartGameButton;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables

}
